#include "GameOverState.h"
#include "Game.h"
#include "MenuState.h"
#include "TextureManager.h"
#include "MenuButton.h"
#include "PlayState.h"
#include "AnimatedGraphic.h"
#include "InputHandler.h"
#include "StateParser.h"

const std::string GameOverState::s_gameOverID = "GAMEOVER";

void GameOverState::s_gameOverToMain(){
    std::cout << "change to game over to main " << std::endl;
#if 0
    TheGame::Instance()->getStateMachine()
                       ->changeState(new MenuState());
#endif
}

void GameOverState::s_restartPlay(){
#if 0
    TheGame::Instance()->getStateMachine()
                       ->changeState(new PlayState());
#endif
    std::cout << " change to restart play " << std::endl;
}

void GameOverState::setCallbacks(const std::vector<Callback> & callbacks){
    for ( int i = 0; i < m_gameObjects.size(); i++)
    {
        if (dynamic_cast<MenuButton*>(m_gameObjects[i])){
            MenuButton * pButton =
            dynamic_cast<MenuButton*>(m_gameObjects[i]);
            pButton->setCallback(callbacks[pButton->getCallbackID()]);
        }
    }

}

bool GameOverState::onEnter(){
#if 0
    if (!TheTextureManager::Instance()->load(
                "assets/gameover.png",
                "gameovertext",
                TheGame::Instance()->getRenderer()
                )){
        return false;
    }
    if (!TheTextureManager::Instance()->load(
                    "assets/main_menu_button.png",
                    "mainbutton",
                    TheGame::Instance()->getRenderer()
                    )){
            return false;
    }
    if (!TheTextureManager::Instance()->load(
                    "assets/restart_button.png",
                    "restartbutton",
                    TheGame::Instance()->getRenderer()
                    )){
            return false;
    }

    GameObject * gameOverText = new AnimatedGraphic(
            new LoaderParams(200, 100,
                             190, 30,
                             "gameovertext"),
            2
            );
    GameObject * main_button = new MenuButton(
            new LoaderParams(200, 200,
                             200, 80,
                             "mainbutton"
                ),
            s_gameOverToMain
            );
    GameObject * restart_button = new MenuButton(
            new LoaderParams(200, 300,
                             200, 80,
                             "restartbutton"
                ),
            s_restartPlay
            );
    
    m_gameObjects.push_back(gameOverText);
    m_gameObjects.push_back(main_button);
    m_gameObjects.push_back(restart_button);
#endif
    StateParser stateParser;
    stateParser.parseState(
            "assets/test.xml",
            s_gameOverID,
            &m_gameObjects,
            &m_textureIDList
            );
    m_callbacks.push_back(0);
    m_callbacks.push_back(s_gameOverToMain);
    m_callbacks.push_back(s_restartPlay);
    setCallbacks(m_callbacks);

    std::cout << "entering GameOverState" << std::endl;
    return true;
}

bool GameOverState::onExit(){
    for (int i = 0; i < m_gameObjects.size(); i++){
        m_gameObjects[i]->clean();
    }
    m_gameObjects.clear();
    TheTextureManager::Instance()
        ->clearFromTextureMap("gameovertext");
    TheTextureManager::Instance()
        ->clearFromTextureMap("mainbutton");
    TheTextureManager::Instance()
        ->clearFromTextureMap("restartbutton");
    TheInputHandler::Instance()->reset();

    std::cout << "exiting GameOverState"<< std::endl;
    return true;

}

void GameOverState::update() {
    for (int i = 0; i < m_gameObjects.size(); i++){
        m_gameObjects[i]->update();
    }

}

void GameOverState::render() {
    for(int i = 0; i <m_gameObjects.size(); i++){
        m_gameObjects[i]->draw();
    }
}

